package app.fugema.shop.main.goods.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyGridLayoutManager;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.ada.GeneralRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import java.util.List;

import app.fugema.shop.R;
import app.fugema.shop.databinding.GoodsClassifyDetailActivityBinding;
import app.fugema.shop.main.goods.adapter.LinerGoodsItemBaseItemMultiType;
import app.fugema.shop.main.goods.adapter.ListProductAdapter;
import app.fugema.shop.main.goods.model.CateGoodsPageBean;
import app.fugema.shop.main.goods.model.GoodsSearchParams;
import app.fugema.shop.main.goods.vm.GoodsVM;
import app.fugema.shop.main.home.adapter.CateGoodsItemBaseItemMultiType;

public class GoodsClassifyDetailActivity extends BaseTitleActivity<GoodsClassifyDetailActivityBinding, GoodsVM> implements OnGetDataListener {

    private boolean isGrid = true;
//    private MyGridLayoutManager mGridLayoutMgr;
//    private MyLinearLayoutManager mLinearLayoutMgr;

    public static void start(Context context, int categoryId, String title) {
        Intent intent = new Intent(context, GoodsClassifyDetailActivity.class);
        intent.putExtra("categoryId", categoryId);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_classify_detail_activity);
    }

    private int categoryId;//当前的分类id
    private GeneralRecyclerAdapter adapter;

    private String title;

    private ListProductAdapter pAdapter;

//    private GoodsSearchParams searchParams;

    private int orderType = 1; ///排序方式（1：升序，2：降序）
    private int orderkey = 1; //排序项（1：默认，2：销量，3：价格，4：评论数，5：上架时间）


    @Override
    protected int getStatusBarColor() {
        return R.color.redColor;
    }


    @Override
    protected void initView() {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        initSortView();
        adapter = new GeneralRecyclerAdapter() {
            @Override
            public void initMultiItemType() {

                putMultiItemType(new CateGoodsItemBaseItemMultiType() {
                    @Override
                    public boolean isCurrentChildItemType(int position, @NonNull CateGoodsPageBean.ProductBean bean) {
                        return isGrid;
                    }
                });

                putMultiItemType(new LinerGoodsItemBaseItemMultiType() {
                    @Override
                    public boolean isCurrentChildItemType(int position, @NonNull CateGoodsPageBean.ProductBean bean) {
                        return !isGrid;
                    }
                });
            }
        };

//        mGridLayoutMgr = new MyGridLayoutManager(this, 2);
//        mLinearLayoutMgr = new MyLinearLayoutManager(this);

        pAdapter = new ListProductAdapter(R.layout.item_cate_goods_view);

        binding.rvRecyclerView.setAdapter(adapter);
//        binding.rvRecyclerView.setLayoutManager(mGridLayoutMgr);
        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);

        binding.ivListType.setColorFilter(Color.parseColor("#FE1439"), PorterDuff.Mode.SRC_ATOP);


        binding.ivListType.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                changeListType();
            }
        });

        binding.tvBtnDef.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                orderType = 1;
                orderkey = 1;
                initSortView();
                onGetData(1);
            }
        });
        //
        binding.llSaleGroup.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (orderkey == 2) {
                    orderType = orderType == 1 ? 0 : 1;
                }
                //第一次点击
                else {
                    orderkey = 2;
                    orderType = 2;//默认降序
                }
                initSortView();
                onGetData(1);
            }
        });
        binding.llEvaluateGroup.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (orderkey == 4) {
                    orderType = orderType == 1 ? 0 : 1;
                }
                //第一次点击
                else {
                    orderkey = 4;
                    orderType = 1;//默认升序
                }
                initSortView();
                onGetData(1);
            }
        });
        binding.llPriceGroup.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (orderkey == 3) {
                    orderType = orderType == 1 ? 0 : 1;
                }
                //第一次点击
                else {
                    orderkey = 3;
                    orderType = 1;//默认升序
                }
                initSortView();
                onGetData(1);
            }
        });

    }


    private void initSortView() {

        switch (orderkey) {
            case 1://默认的
                orderType = 1;//默认升序
                binding.tvBtnDef.setSelected(true);
                binding.tvBtnSale.setSelected(false);
                binding.tvEvaluate.setSelected(false);
                binding.tvBtnPrice.setSelected(false);


                binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleTop.setRotation(0);
                binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleDown.setRotation(180);

                binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateTop.setRotation(0);
                binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateDown.setRotation(180);

                binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceTop.setRotation(0);
                binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceDown.setRotation(180);

                break;
            case 2:// 销量

//                orderType = 2;//默认降序
                binding.tvBtnDef.setSelected(false);
                binding.tvBtnSale.setSelected(true);
                binding.tvEvaluate.setSelected(false);
                binding.tvBtnPrice.setSelected(false);


                // 排序(1升序 2降序)
                if (orderType == 1) {
                    binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivSaleTop.setRotation(180);
                    binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivSaleDown.setRotation(180);
                } else {
                    binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivSaleTop.setRotation(0);
                    binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivSaleDown.setRotation(0);
                }


                binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateTop.setRotation(0);
                binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateDown.setRotation(180);

                binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceTop.setRotation(0);
                binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceDown.setRotation(180);

                break;
            case 4://评价
//                orderType = 1;//默认升序
                binding.tvBtnDef.setSelected(false);
                binding.tvBtnSale.setSelected(false);
                binding.tvEvaluate.setSelected(true);
                binding.tvBtnPrice.setSelected(false);


                binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleTop.setRotation(0);
                binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleDown.setRotation(180);

                // 排序(1升序 2降序)
                if (orderType == 1) {
                    binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivEvaluateTop.setRotation(180);
                    binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivEvaluateDown.setRotation(180);
                } else {
                    binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivEvaluateTop.setRotation(0);
                    binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivEvaluateDown.setRotation(0);
                }

                binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceTop.setRotation(0);
                binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceDown.setRotation(180);


                break;
            case 3://价格
//                orderType = 1;//默认升序
                binding.tvBtnDef.setSelected(false);
                binding.tvBtnSale.setSelected(false);
                binding.tvEvaluate.setSelected(false);
                binding.tvBtnPrice.setSelected(true);


                binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleTop.setRotation(0);
                binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleDown.setRotation(180);

                binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateTop.setRotation(0);
                binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateDown.setRotation(180);


                // 排序(1升序 2降序)
                if (orderType == 1) {
                    binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivPriceTop.setRotation(180);
                    binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivPriceDown.setRotation(180);
                } else {
                    binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivPriceTop.setRotation(0);
                    binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivPriceDown.setRotation(0);
                }
                break;
        }
    }

    @Override
    protected void initData(Intent intent) {
        super.initData(intent);
        title = intent.getStringExtra("title");
        setTitle(title, R.color.white);
        categoryId = intent.getIntExtra("categoryId", 0);

        onGetData(1);
    }

    @Override
    public void onGetData(int pn) {

        GoodsSearchParams searchParams=new GoodsSearchParams();
        searchParams.setPageno(pn);
        searchParams.setCid(categoryId);
        searchParams.setOrderkey(orderkey);
        searchParams.setOrdertype(orderType);
        if (LoginUtils.isLogin()) {
            searchParams.setUserkey(LoginUtils.getToken());
        }
        //viewModel.getPageGoodsByCateId(categoryId, pn)
        viewModel.getPageGoodsByParams(searchParams).observe(this, new BaseViewObserver<LiveDataWrapper<CateGoodsPageBean>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<CateGoodsPageBean> data) {
                if (UIUtils.isNotEmpty(data.data.getProduct())) {
                    for (CateGoodsPageBean.ProductBean tempProduct : data.data.getProduct()) {
                        //tempProduct.setItemType(BaseItemTypeInterface.TYPE_HEAD_ONE);
                    }
                }
                viewUtils.setDataListRefreshLayout(adapter, pn, 10, data.data.getProduct());
            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<CateGoodsPageBean> data) {
                // || (data.data.getData().getCurrent() == 1 && UIUtils.isEmpty(data.data.getData().getRecords())
                return pn == 1 && UIUtils.isEmpty(data.data.getProduct());
            }
        });

    }


    private void changeListType() {
        isGrid = !isGrid;
        if (isGrid) {
            binding.ivListType.setImageResource(R.mipmap.ic_grid_list);
        } else {
            binding.ivListType.setImageResource(R.mipmap.ic_linear_list);

        }
        adapter.notifyDataSetChanged();
    }

}
